// pages/hotel/hotel.js
const app = getApp()
var wh = require("../../utils/wh.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bargainImg: [],
    modal: false,
    shop_id:'',
    ShopDetail:'',
    is_close:'',
    page:1,
    morelists:[],
    page_size:5,
    no_data: 0,
    onearth: false, // 是否已经到达底部
    have:true
  },
  rest() {
    this.setData({
      is_close:0
    })
  },
  // 店铺商品
  morelists(shop_id) {
    let that = this;
    let page_size = this.data.page_size,
      no_data = this.data.no_data,
      page = this.data.page;
    if (no_data == 0) {
    wh.Post('/api/mall.goods/lists', {
      shop_id: shop_id,
      page: page,
      num: page_size
    }, function (res) {
      let shop = that.data.morelists.concat(res),
        count = res.length;
      if (page_size > count) {
        that.data.no_data = 1;
        if (shop.length > 3) {
          that.setData({
            onearth: true
          })
        }
      }
      that.setData({
        morelists: shop
      })
      if (that.data.morelists.length == 0) {
        that.setData({
          have: false,
        });
      } else {
        that.setData({
          have: true,
        });
      }
      console.log('更多经典', res)
    })
    }
  },
  // 获取用户足迹展示列表
  getFootList(shop_id) {
    let that = this;
    wh.Post('/api/mall.footmark/lists', {
      shop_id: shop_id,
      target_id: shop_id,
      type: 1
    }, function (res) {
      console.log('足迹', res)
      that.setData({
        footprint: res
      })
    });
  },
  // 记录用户访问足迹
  FootMarkRecord(shop_id) {
    wh.Post('/api/mall.footmark/record', {
      shop_id: shop_id,
      target_id: shop_id,
      type: 1
    }, function (res) {

    })
  },
  // 获取商家店铺详情
  getShopDetail(shop_id) {
    var that = this;
    wh.Post('/api/mall.shop/detail', {
      shop_id: shop_id
    }, function (res) {
      that.getFootList(shop_id)
      // that.FootMarkRecord(shop_id)
      console.log('店铺详情', res)
      that.setData({
        ShopDetail: res,
        is_close: res.is_close
      })
    });
  },
  // 打电话
  call: function () {
    let that = this;
    let tell = that.data.ShopDetail.tell;

    if (tell == '' || tell == undefined) {

      wx.showModal({
        title: '提示',
        content: '联系电话未设置，不能联系',
        showCancel: false
      })
      return;
    }
    wx.makePhoneCall({
      phoneNumber: tell,
    })
  },
  // 导航
  go: function () {
    var that = this;
    let setting = that.data.ShopDetail;
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度  
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        var latitudeIdx = Number(setting.latitude);
        var longitudeIdx = Number(setting.longitude);
        var map_name = setting.shop_name;
        wx.openLocation({
          latitude: latitudeIdx,
          longitude: longitudeIdx,
          name: map_name,
          scale: 15
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options.shop_id)
    this.getShopDetail(options.shop_id)
    this.morelists(options.shop_id)
      this.setData({
        shop_id: options.shop_id
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let shop_id = this.data.shop_id;
    this.setData({
      no_data: 0,
      page: 1,
      page_size: 5,
      onearth: false,
      have: true,
      morelists: [],
    });
    this.morelists(shop_id)
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let shop_id = this.data.shop_id;
    let no_data = this.data.no_data,
      page = this.data.page,
      page_size = this.data.page_size;
    if (no_data == 0) {
      this.data.page = page + 1;
      this.morelists(shop_id)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    let that = this;
    return {
      title: that.data.ShopDetail.shop_name,
      path: '/pages/hotel/hotel?shop_id=' + that.data.shop_id,
      imageUrl: that.data.ShopDetail.shop_cover,
      success: function (res) {
        console.log('转发成功')
      }
    }
  }
})